package main

import (
	"context"
	"fmt"
	"log"

	"github.com/micro/go-micro"
	"github.com/sqs/goreturns/returns"

	proto "eg3/proto"
)

// 只执行一次之后就销毁服务，如果需要再次执行需要重新的调用开启服务
type Greeter struct{}

func (g *Greeter) Hello(ctx context.Context, req *proto.HelloRequest, rsp *proto.HelloResponse) error {
	rsp.Greeting = "hello" + req.Name
	return nil
}

func main() {
	service := micro.NewService(
		micro.Name("greeter"),
		micro.Version("latest"),
	)
	service.Init()

	proto.RegisterGreeterHandler(service.Server(), new(Greeter))

	if err = service.Options().Registry.Register(); err !=nil{
		fmt.Println("register")
		return
	}
	for _, fn := service.Options().BeforeStart{
		fmt.Println("beforeStart")
		if err := fn(); err!=nil{
			return err
		}
	}

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
	
	// if err = service.Options().Server.Start(); err !=nil{
	// 	fmt.Println("start")
	// 	return
	// }

	for _, fn := range service.Options().AfterStart {
		fmt.Println("after")
		if err := fn(); err != nil {
				return err
		}
}

}
